#pragma once
#include<websocketpp/server.hpp>
#include<websocketpp/config/asio_no_tls.hpp>
#include<mutex>
#include<unordered_map>
#include"util.hpp"
class online_manager
{
    public:
    //websocket连接建立时执行
    void enter_game_hall(uint64_t uid,websocket_server::connection_ptr& conn)//ptr为shared_ptr
    {
        std::unique_lock<std::mutex> guard(_mutex);
        _hall_user.insert(std::make_pair(uid,conn));
    }
    void enter_game_room(uint64_t uid,websocket_server::connection_ptr& conn)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        _room_user.insert(std::make_pair(uid,conn));
    }
    //websocket连接断开时执行
    void exit_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        _hall_user.erase(uid);
    }
    void exit_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        _room_user.erase(uid);
    }
    //判断当前用户是否在游戏大厅/房间
    bool is_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        auto it=_hall_user.find(uid);
        if(it==_hall_user.end())
            return false;
        return true;
    }
    bool is_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        auto it=_room_user.find(uid);
        if(it==_room_user.end())
            return false;
        return true;
    }
    websocket_server::connection_ptr get_conn_from_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        auto it=_hall_user.find(uid);
        if(it==_hall_user.end())
            return websocket_server::connection_ptr();
        return it->second;
    }
    websocket_server::connection_ptr get_conn_from_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> guard(_mutex);
        auto it=_room_user.find(uid);
        if(it==_room_user.end())
            return websocket_server::connection_ptr();
        return it->second;
    }
    private:
    std::mutex _mutex;
    std::unordered_map<uint64_t,websocket_server::connection_ptr> _hall_user;
    std::unordered_map<uint64_t,websocket_server::connection_ptr> _room_user;
};